package com.htx.zqs.blesmartmask.ui.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.htx.zqs.blesmartmask.R;
import com.htx.zqs.blesmartmask.utils.Constants;
import com.htx.zqs.blesmartmask.utils.FileUtils;
import com.htx.zqs.blesmartmask.utils.Utils;
import com.tencent.smtt.sdk.TbsReaderView;

import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;

/**
 * Created by KomoriWu
 * on 2019/8/19.
 */
public class InstructionsActivity extends BaseActivity implements TbsReaderView.ReaderCallback {
    @InjectView(R.id.ib_back)
    ImageButton ibBack;
    @InjectView(R.id.tv_title)
    TextView tvTitle;
    @InjectView(R.id.layout_content)
    RelativeLayout layoutContent;
    TbsReaderView tbsReaderView;
    private String pathName;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_instructions);
        ButterKnife.inject(this);
        tbsReaderView = new TbsReaderView(this, this);
        layoutContent.addView(tbsReaderView, new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
                RelativeLayout.LayoutParams.MATCH_PARENT));
        boolean isCN=getResources().getConfiguration().locale.getCountry().equals("CN");
        pathName = isCN ? "instructions.pptx" : "instructions_en.pptx";
        initView();
    }

    private void initView() {
        ibBack.setVisibility(View.VISIBLE);
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(R.string.instructions);
        FileUtils.getInstance(getApplicationContext()).copyAssetsToSD("instructions",
                "smartmask/instructions").setFileOperateCallback(new FileUtils.FileOperateCallback() {
            @Override
            public void onSuccess() {
                Bundle bundle = new Bundle();
                bundle.putString("filePath", "/storage/emulated/0/smartmask/instructions/"+pathName);
                bundle.putString("tempPath", Environment.getExternalStorageDirectory().getPath());
                boolean result = tbsReaderView.preOpen(parseFormat("instructions.pptx"), false);
                if (result) {
                    tbsReaderView.openFile(bundle);
                }
            }

            @Override
            public void onFailed(String error) {

            }
        });
    }

    private String parseFormat(String fileName) {
        return fileName.substring(fileName.lastIndexOf(".") + 1);
    }

    @OnClick(R.id.ib_back)
    public void onViewClicked() {
        finish();
    }

    public static void startAct(Context context) {
        Intent intent = new Intent(context, InstructionsActivity.class);
        context.startActivity(intent);
    }

    @Override
    public void onCallBackAction(Integer integer, Object o, Object o1) {

    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        tbsReaderView.onStop();
    }
}
